(function(){
    angular.module('feibo.teacher.controller').controller('TeacherContractAddController', TeacherContractAddController);

    TeacherContractAddController.$inject = ['$scope', '$stateParams', '$state', 'TeacherService', 'valiFrom', 'kvMap', 'CommonService'];

    function TeacherContractAddController($scope, $stateParams, $state, TeacherService, valiFrom, kvMap, CommonService){
        var vm = this;

        vm.kvMap = {
            shiftTimeSlotType: CommonService.cloneMapping(kvMap.shiftTimeSlotType, ['vkMap']),
            shiftCategoryTimeSlot: CommonService.cloneMapping(kvMap.shiftCategoryTimeSlot, ['vkMap']),
            unit: CommonService.cloneMapping(kvMap.buyoutUnit, ['vkMap']),
            teacher_type: CommonService.cloneMapping(kvMap.teacherType, ['vkMap'])
        };
        vm.data = {
            contractId: $stateParams.contractId,
            bigClassTypeList: undefined,
            smallClassTypeList: undefined,
            formData: {
                school_id: undefined,
                contract_no: undefined,
                signed_date: undefined,
                termination_date: undefined,
                notes: undefined
            },
            teacherPacks: undefined,  // 修改操作时存放专属组列表
            bigClassFormData: {
                name: undefined,  // 班型大类编号
                wd_days: undefined,  // 平日工作天数
                we_days: undefined,  // 周末工作天数
                wd_hours: undefined,  // 平日工作时长
                we_hours: undefined,  // 周末工作时长
                monday_time: '1',
                tuesday_time: '1',
                wednesday_time: '1',
                thursday_time: '1',
                friday_time: '1',
                saturday_time: '1',
                sunday_time: '1'
            },
            smallClassModalData: {
                parent: {
                    name: undefined,
                    id: undefined,
                    wd_days: undefined,
                    wd_hours: undefined,
                    we_days: undefined,
                    we_hours: undefined,
                    monday_time: undefined,
                    tuesday_time: undefined,
                    wednesday_time: undefined,
                    thursday_time: undefined,
                    friday_time: undefined,
                    saturday_time: undefined,
                    sunday_time: undefined
                },
                name: undefined,
                id: undefined,
                timeItemsData: undefined
            },
            groupItemsData: undefined,
            target: undefined,
            timePoints: undefined,  // 班型小类时间点数组
            weeks: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
        };

        vm.searchSchool = {
            url: ENDPOINTS.school.search + '?page_size=10&search={query}',
            container: vm.data.formData,
            propName: 'school_id',
            propChain: ['entries'],
            resultsPropsConfig: {
                title: ['name'],
                value: ['id']
            }
        };

        vm.initData = function(){
            vm.initEvent();
            if(vm.data.contractId){
                vm.getTeacherContract(vm.data.contractId);
            }
            vm.fetchClassType();
            vm.creatTimePoint(6, 23);
        };

        vm.initEvent = function(){
            var datetimepickers = ['#datetimepicker1', '#datetimepicker2'];
            vm.initDatetimepicker(datetimepickers);

            $('#group-container').on('click', '[button]', function(event){
                var target = vm.data.target = event.target;
                var operation = target.getAttribute('button');

                switch(operation){
                    case 'delete-group-item':
                        var layerIndex = layer.confirm('确定要删除当前老师专属组', {
                            btn: ['确定','取消']
                        }, function(){
                            layer.close(layerIndex);
                            vm.deleteGroupItem(target);
                        });
                        break;
                    // case 'add-big-class':
                    // case 'add-small-class':
                    default :
                        vm.showModal(operation);
                        break;
                }
            });
        };

        vm.initDatetimepicker = function(selectorArr){
            if(angular.isArray(selectorArr)){
                selectorArr.forEach(function(val){
                    $(val).datetimepicker({
                        lang: 'ch',
                        format: 'Y-m-d',
                        timepicker: false,
                        yearStart: 2000,
                        yearEnd: 2050,
                        step: 10
                    });
                });
            }
        };

        vm.filterfiled = function(data){
            var resultData = {};
            Object.keys(data).forEach(function(key){
                if(data[key] !== undefined){
                    resultData[key] = data[key];
                }
            });

            return resultData;
        };

        // 添加老师专属组
        vm.addGroupItem = function(){
            var $tpl = $($('#fb-tpl').html());

            // 初始化日历
            var datetimepickers = [$tpl.find('input[name=start_date]'), $tpl.find('input[name=end_date]')];
            vm.initDatetimepicker(datetimepickers);

            // 初始化 价格单位 师型 课型 dropdown
            initDropDown('teacher_type', 'enUS');
            initDropDown('unit');
            $tpl.find('.teacher_type').dropdown();
            $tpl.find('.unit').dropdown();
            $tpl.find('.class_type').dropdown();


            function initDropDown(className, keyName){
                keyName = keyName || 'zhCN';
                var optionHtml = '';
                var item = vm.kvMap[className];

                for(var key in item){
                    optionHtml += '<option value="' + key + '">' + item[key][keyName] + '</option>';
                }

                $tpl.find('select.' + className).html(optionHtml);
            }

            // 初始化 班型 dropdown
            var $bigClassType = $tpl.find('.ui.dropdown[name=group_big_class]');
            vm.updateDropdown($bigClassType, vm.data.bigClassTypeList, true);
            if(vm.data.bigClassTypeList[0]){
                vm.data.bigClassTypeList[0].shift_type.unshift();

                vm.updateDropdown(
                    $tpl.find('.ui.dropdown[name=group_small_class]'),
                    [{id: 0, name: ''}].concat(vm.data.bigClassTypeList[0].shift_type)
                );

                var bigClass = vm.data.smallClassModalData.parent;
                Object.keys(bigClass).forEach(function(key){
                    this[key] = vm.data.bigClassTypeList[0][key];
                }, bigClass);
            }else {
                vm.updateDropdown(
                    $tpl.find('.ui.dropdown[name=group_small_class]'),
                    [{id: 0, name: ''}].concat([])
                );
            }

            // 给班型大类添加 change 监听
            $bigClassType.on('change', vm.changeBigClassTypeDropdown);
            $('#group-container').append($tpl);
        };

        // 更新 班型大类和小类 dropdown 数据 并初始化
        vm.updateDropdown = function($target, data, isBigClass){
            var optionHtml = isBigClass ? '' : '<option value="0">&nbsp;</option>';
            if(angular.isArray(data)){
                data.forEach(function(val, key){
                    // index 在 大类 change 之后 通过 index 去查找大类下面的小类
                    optionHtml += '<option index="' + key + '" value="' + val.id + '">' + val.name + '</option>';
                });
                $target.empty();
                $target.append(optionHtml);
            }
            $target.dropdown();
        };

        // 添加大类或小类之后 1.添加 option 到 select  2.push 新 数据到大类列表
        vm.addClassAfter = function(id, isSmall){
            var target = vm.data.target;
            var parent = target.parentNode;
            var dataList = vm.data.bigClassTypeList;
            if(isSmall){
                var index = $(parent.parentNode.previousElementSibling).find('select')[0].selectedOptions[0].getAttribute('index');
                var data1 = {
                    id: id,
                    name: vm.data.smallClassModalData.name,
                    fb_shift_category_id: dataList[index].id
                };

                $(parent).find('select').append('<option index="' +
                        dataList[index].shift_type.length + '" value="' + id + '">' + data1.name + '</option>');

                dataList[index].shift_type.push(data1);
            }else {
                var data2 = angular.copy(vm.data.bigClassFormData);

                data2.id = id;
                data2.shift_type = [];

                $(parent).find('select').append('<option index="' +
                        dataList.length + '" value="' + id + '">' + data2.name + '</option>');

                dataList.push(data2);
            }
        };

        // 改变班型大类的时候更新班型小类
        vm.changeBigClassTypeDropdown = function(event){
            var target = event.target;
            var selectedOption = target.selectedOptions[0];
            if(selectedOption){
                var index = selectedOption.getAttribute('index');
                var $parent = $(target.parentNode.parentNode.parentNode.nextElementSibling);
                $parent.find('div.text').html('');
                vm.updateDropdown($parent.find('select[name=group_small_class]'), vm.data.bigClassTypeList[index].shift_type);
            }
        };

        // 删除当前老师专属组
        vm.deleteGroupItem = function(target){
            var key = target.getAttribute('key');
            var groupItem = target.parentNode.parentNode.parentNode;
            var groupContainer = groupItem.parentNode;
            groupContainer.removeChild(groupItem);

            if(key){
                vm.deletePack(key);
            }
        };

        // 删除专属合同
        vm.deleteContract = function(){
            if(vm.getGroupsValue().length){
                // 专属组还有数据
                CommonService.showTips('专属组还有数据，请删除完再删除合同', 'warn');
            }else {
                // 删除
                vm.deleteTeacherContract();
            }
        };

        // 获取老师专属组里的所有数据
        vm.getGroupsValue = function(){
            var $items = $('#group-container > .group-item');
            var resData = [];
            vm.data.groupItemsData = resData;
            $items.each(function(key, node){
                var $node = $(node);
                var data = {
                    start_date: undefined,
                    end_date: undefined,
                    price: undefined,
                    number_of_teachers: undefined
                };
                Object.keys(data).forEach(function(key){
                    var temp = $node.find('input[name=' + key + ']').val();
                    if(temp){
                        this[key] = temp;
                    }
                }, data);

                ['teacher_type', 'class_type', 'unit', 'fb_shift_category_id', 'fb_shift_type_id'].forEach(function(key){
                    data[key] = $node.find('.' + key).dropdown('get value');
                });

                data.key = $node.find('span[button=delete-group-item]').attr('key');

                resData.push(data);
            });

            return resData;
        };

        // 模态框
        vm.showModal = function(name){
            var parent = vm.data.smallClassModalData.parent;
            if(name === 'add-small-class' && vm.data.bigClassTypeList.length === 0){
                CommonService.showTips('请先添加大类，再添加小类', 'warn');
                return;
            }else if(name === 'add-small-class'){
                var index = $(vm.data.target.parentNode.parentNode.previousElementSibling).find('select')[0].
                        selectedOptions[0].getAttribute('index');
                var bigClass = vm.data.smallClassModalData.parent;
                $scope.$apply(function(){
                    Object.keys(bigClass).forEach(function(key){
                        this[key] = vm.data.bigClassTypeList[index][key];
                    }, bigClass);
                });
            }

            $('#' + name + '-modal').modal({
                closable: false
            }).modal('show');
        };
        vm.hideModal = function(name){
            $('#' + name).modal('hide');
            vm.clearForm(name + '-form');
        };

        vm.clearForm = function(formId){
            setTimeout(function(){
                $('#' + formId).form('reset');
                $('.ui.error.message').html('');
            });
        };

        // 添加或修改老师合同之后 更新或添加专属组
        vm.addOrUpdatePack = function(returnedData){
            var contractId;
            if(returnedData.success){
                // 获取专属组数据
                vm.getGroupsValue();
                if(returnedData.data){
                    contractId = vm.data.contractId  = returnedData.data.id;
                }else {
                    contractId = vm.data.contractId;
                }

                // 遍历所有的专属组
                vm.data.groupItemsData.forEach(function(val, index){
                    if(val.key){
                        // 更新专属组
                        vm.updatePack(val, contractId);
                    }else {
                        // 添加专属组 index 为删除按钮打标记
                        vm.addTeacherPack(val, contractId, index);
                    }
                });
            }
        };

        // 获取班型大类
        vm.fetchClassType = function(isFirst){
            var queryConfig = {
                funcName: 'fetchClassType',
                container: vm.data,
                propName: 'bigClassTypeList',
                propChain: ['data', 'entries'],
                showLoadLayer: false
            };

            TeacherService.fetchClassType(queryConfig).then(function(returnedData){
                if(returnedData.success && isFirst){
                    vm.updateDropdown($(vm.data.target.parentNode).find('select'), returnedData.data.entries);
                }
            }).catch(function(error){
                console.error(error);
            });
        };

        // 获取单个专属合同
        vm.getTeacherContract = function(id){
            var queryConfig = {
                funcName: 'getTeacherContract',
                container: vm.data,
                propName: 'teacherPacks',
                propChain: ['data', 'teacher_pack'],
                showLoadLayer: false,
                urlPlaceholder: {
                    id: id
                }
            };

            TeacherService.getTeacherContract(queryConfig).then(function(returnedData){
                var formData = vm.data.formData;
                if(returnedData.success){
                    Object.keys(formData).forEach(function(key){
                        formData[key] = returnedData.data[key];
                    });

                    $('#school').find('.search input').val(returnedData.data.school.name);

                    returnedData.data.teacher_pack && returnedData.data.teacher_pack.forEach(function(val, index){
                        setTimeout(function(){
                            // 初始化下拉框 和 日期框
                            vm.initDatetimepicker(['#' + index + '-datetimepicker1', '#' + index + '-datetimepicker2']);
                            ['teacher_type', 'class_type', 'unit'].forEach(function(key){
                                $('#' + index + '-dropdown-' + key).dropdown('set selected', val[key].toString());
                            });
                            // 初始化大类 和 小类下拉框
                            $('#' + index + '-dropdown-fb_shift_category').on('change', vm.changeBigClassTypeDropdown)
                                    .dropdown('set selected', val.fb_shift_category_id);
                            if(val.fb_shift_type){
                                $('#' + index + '-dropdown-shift_type').dropdown('set selected', val.fb_shift_type_id);
                            }else {
                                $('#' + index + '-dropdown-shift_type').dropdown();
                            }
                        });
                    });
                }
            }).catch(function(error){
                console.error(error);
            });
        };

        // 添加专属老师合同
        vm.addTeacherContract = function(){
            var data = vm.data;
            var postConfig = {
                funcName: 'addTeacherContract',
                successMsg: '老师专属合同添加成功',
                errorMsg: '老师专属合同添加失败',
                showLoadLayer: false,
                httpPostData: data.formData
            };

            TeacherService.addTeacherContract(postConfig).then(function(returnedData){
                vm.addOrUpdatePack(returnedData);
            }).catch(function(error){
                console.error(error);
            });
        };

        // 修改专属老师合同
        vm.updateTeacherContract = function(){
            var data = vm.data;
            var putConfig = {
                funcName: 'updateTeacherContract',
                successMsg: '老师专属合同修改成功',
                errorMsg: '老师专属合同修改失败',
                showLoadLayer: false,
                httpPutData: data.formData,
                urlPlaceholder: {
                    id: data.contractId
                }
            };

            TeacherService.updateTeacherContract(putConfig).then(function(returnedData){
                vm.addOrUpdatePack(returnedData);
            }).catch(function(error){
                console.error(error);
            });
        };

        // 删除专属合同
        vm.deleteTeacherContract = function(){
            var deleteConfig = {
                funcName: 'deleteTeacherContract',
                successMsg: '老师专属合同删除成功',
                errorMsg: '老师专属合同删除失败',
                showLoadLayer: false,
                urlPlaceholder: {
                    id: vm.data.contractId
                }
            };

            TeacherService.deleteTeacherContract(deleteConfig).then(function(returnedData){
                if(returnedData.success){
                    $state.go('teacher-contract-mgr');
                }
            }).catch(function(error){
                console.error(error);
            });
        };

        // 添加专属老师组
        vm.addTeacherPack = function(data, contractId, index){
            var postConfig = {
                funcName: 'addTeacherPack',
                successMsg: '专属老师组添加成功',
                errorMsg: '专属老师组添加失败',
                showLoadLayer: false,
                httpPostData: vm.filterfiled(data)
            };

            if(contractId){
                postConfig.httpPostData.buyout_teacher_contract_id = contractId;
            }else {
                return;
            }

            TeacherService.addTeacherPack(postConfig).then(function(returnedData){
                var item = $('#group-container > .group-item')[index];
                if(returnedData.success){
                    $(item).find('span[button=delete-group-item]').attr({key: returnedData.data.id});
                }
            }).catch(function(error){
                console.error(error);
            });
        };

        // 删除专属老师组
        vm.deletePack = function(id){
            var deleteConfig = {
                funcName: 'deletePack',
                successMsg: '删除专属老师组成功',
                errorMsg: '删除专属老师组失败',
                showLoadLayer: false,
                urlPlaceholder: {
                    id: id
                }
            };

            TeacherService.deletePack(deleteConfig).catch(function(error){
                console.error(error);
            });
        };

        // 修改专属老师组
        vm.updatePack = function(data, contractId){
            var putConfig = {
                funcName: 'updatePack',
                successMsg: '修改专属老师组成功',
                errorMsg: '修改专属老师组失败',
                showLoadLayer: false,
                httpPutData: vm.filterfiled(data),
                urlPlaceholder: {
                    id: data.key
                }
            };

            if(contractId){
                putConfig.httpPutData.buyout_teacher_contract_id = contractId;
            }else {
                return;
            }

            TeacherService.updatePack(putConfig).catch(function(error){
                console.error(error);
            });
        };

        // 添加班型大类
        vm.addClassType = function(){
            var data = vm.data;
            var postConfig = {
                funcName: 'addClassType',
                successMsg: '班型大类添加成功',
                errorMsg: '班型大类添加失败',
                showLoadLayer: false,
                httpPostData: data.bigClassFormData,
            };

            TeacherService.addClassType(postConfig).then(function(returnedData){
                if(returnedData.success){
                    // 添加成功班型大类之后:
                    // 1. 关闭模态框
                    vm.hideModal('add-big-class-modal');
                    // 2. push 新数据到 大类列表
                    // 3. 追加 option 到 select
                    vm.addClassAfter(returnedData.data.id);
                }
            }).catch(function(error){
                console.error(error);
            });
        };

        valiFrom.extenUniqueVali('uniqueContractNo');
        vm.submitBtn = function(){
            $('#contract-form-add').form('destroy');
            $('#contract-form-add').form({
                fields: {
                    contract_no: {
                        rules: [{
                            type: 'empty',
                            prompt: '请填写合同号(必填项)'
                        },{
                            type: 'uniqueContractNo',
                            prompt: '已存在该合同号，请确保合同号的唯一性'
                        }]
                    },
                    school_id: {
                        rules: [{
                            type: 'empty',
                            prompt: '请选择一个机构(必选项)'
                        }]
                    },
                    signed_date: {
                        rules: [{
                            type: 'empty',
                            prompt: '请选择签署日期(必选项)'
                        }]
                    },
                    notes: {
                        optional: true,
                        rules: [{
                            type: 'maxLength[45]',
                            prompt: '请控制备注的长度(不能超过45个字)'
                        }]
                    },
                    start_date: {
                        rules: [{
                            type: 'empty',
                            prompt: '请选择开始时间(必选项)'
                        }]
                    },
                    end_date: {
                        rules: [{
                            type: 'empty',
                            prompt: '请选择结束时间(必选项)'
                        }]
                    },
                    number_of_teachers: {
                        optional: true,
                        rules: [{
                            type: 'integer[0..]',
                            prompt: '请输入正确的老师数量(自然数)'
                        }]
                    },
                    price: {
                        optional: true,
                        rules: [{
                            type: 'number',
                            prompt: '请输入正确格式的价格(数字)'
                        }]
                    },
                    group_big_class: {
                        rules: [{
                            type: 'empty',
                            prompt: '请选择班课大类(必选项)'
                        }]
                    }
                },
                onSuccess: function(){
                    vm.data.formData.originalData && delete vm.data.formData.originalData;
                    if(vm.data.contractId){
                        vm.updateTeacherContract();
                    }else {
                        vm.addTeacherContract();
                    }
                    return false;
                }
            });
        };

        valiFrom.extenUniqueVali('uniqueShiftCategory');
        $('#add-big-class-form').form({
            fields: {
                class_number: {
                    rules: [{
                        type: 'empty',
                        prompt: '请输入大类编号(必填项)'
                    },{
                        type: 'uniqueShiftCategory',
                        prompt: '已存在该大类编号，请确保大类编号的唯一性'
                    }]
                },
                wd_days: {
                    rules: [{
                        type: 'empty',
                        prompt: '请填写周一到周五工作天数(必填项)'
                    },{
                        type: 'integer[0..]',
                        prompt: '请输入正确的周一到周五工作天数(自然数)'
                    }]
                },
                wd_hours: {
                    rules: [{
                        type: 'empty',
                        prompt: '请填写周一到周五工作时长(必填项)'
                    },{
                        type: 'integer[0..]',
                        prompt: '请输入正确的周一到周五工作时长(自然数)'
                    }]
                },
                we_days: {
                    rules: [{
                        type: 'empty',
                        prompt: '请填写周末工作天数(必填项)'
                    },{
                        type: 'integer[0..]',
                        prompt: '请输入正确的周末工作天数(自然数)'
                    }]
                },
                we_hours: {
                    rules: [{
                        type: 'empty',
                        prompt: '请填写周末工作时长(必填项)'
                    },{
                        type: 'integer[0..]',
                        prompt: '请输入正确的周末工作时长(自然数)'
                    }]
                },
                monday_time: {
                    rules: [{
                        type: 'empty',
                        prompt: '请选择周一选项(必选项)'
                    }]
                },
                tuesday_time: {
                    rules: [{
                        type: 'empty',
                        prompt: '请选择周二选项(必选项)'
                    }]
                },
                wednesday_time: {
                    rules: [{
                        type: 'empty',
                        prompt: '请选择周三选项(必选项)'
                    }]
                },
                thursday_time: {
                    rules: [{
                        type: 'empty',
                        prompt: '请选择周四选项(必选项)'
                    }]
                },
                friday_time: {
                    rules: [{
                        type: 'empty',
                        prompt: '请选择周五选项(必选项)'
                    }]
                },
                saturday_time: {
                    rules: [{
                        type: 'empty',
                        prompt: '请选择周六选项(必选项)'
                    }]
                },
                sunday_time: {
                    rules: [{
                        type: 'empty',
                        prompt: '请选择周日选项(必选项)'
                    }]
                }
            },
            onSuccess: function(){
                vm.addClassType();
                return false;
            }
        });

        valiFrom.extenUniqueVali('uniqueShiftType');
        $('#add-small-class-form').form({
            fields: {
                class_number: {
                    rules: [{
                        type: 'empty',
                        prompt: '请输入小类编号(必填项)'
                    },{
                        type: 'uniqueShiftType',
                        prompt: '已存在该小类编号，请确保小类编号的唯一性'
                    }]
                }
            },
            onSuccess: function(){
                vm.addClassMiniType();
                vm.getSmallClassData();
                return false;
            }
        });

        /* ================ 添加班型小类相关操作 ================ */
        // 生成时间点
        vm.creatTimePoint = function(start, end){
            var points = vm.data.timePoints = [];
            start = window.parseInt(start) || 0;
            end = window.parseInt(end) || 23;

            function fillZero(number){
                return number > 9 ? number : '0' + number;
            }

            for(var i = start; i <= end; i++){
                points.push(fillZero(i) + ':00');
                points.push(fillZero(i) + ':30');
            }
        };

        vm.initTimePointDropdown = function(){
            $('#add-small-class-form .dropdown').dropdown();
        };

        // 添加时间段
        vm.addTimePeriodBtn = function(event){
            var week = event.target.parentNode.parentNode;
            var $tpl = $('#shift-time-slot-tpl').children().clone(true);
            $tpl.find('.dropdown').dropdown();
            $tpl.appendTo(week);
        };

        // 删除时间段
        vm.deleteTimePeriodBtn = function(event){
            $(event.target.parentNode).remove();
        };

        // 获取小类数据
        vm.getSmallClassData = function(){
            var smallClassData = vm.data.smallClassModalData.timeItemsData = [];
            $('#add-small-class-form .day').each(function(key, node){
                var temp = {
                    type: undefined,
                    start_time: undefined,
                    end_time: undefined
                };

                var $node = $(node);

                $node.find('section').each(function(k, n){
                    var $n = $(n);
                    Object.keys(temp).forEach(function(key){
                        temp[key] = Number($n.find('.' + key).dropdown('get value'));
                    });

                    temp.day_of_week = $node.attr('key');
                    for(var i = temp.start_time; i < temp.end_time; i++){
                        var item = {
                            day_of_week: temp.day_of_week,
                            type: temp.type,
                            time_slot: vm.data.timePoints[i]
                        };

                        smallClassData.push(item);
                    }
                });
            });
        };

        // 添加班型小类
        vm.addClassMiniType = function(){
            var data = vm.data;
            var postConfig = {
                funcName: 'addClassMiniType',
                successMsg: '班型小类添加成功',
                errorMsg: '班型小类添加失败',
                showLoadLayer: false,
                httpPostData: {
                    name: data.smallClassModalData.name,
                    fb_shift_category_id: data.smallClassModalData.parent.id
                }
            };

            TeacherService.addClassMiniType(postConfig).then(function(returnedData){
                var shiftTypeId;
                if(returnedData.success){
                    shiftTypeId = returnedData.data.id;
                    // 添加成功之后:
                    // 1: 关闭模态框
                    vm.hideModal('add-small-class-modal');
                    // 2: 将数据添加到下拉框
                    // 3: 将数据添加到大类列表
                    vm.addClassAfter(shiftTypeId, true);

                    // 4. 添加时间段
                    vm.addShiftTimeSlot(shiftTypeId);
                }
            }).catch(function(error){
                console.error(error);
            });
        };

        // 添加班型连续时段
        vm.addShiftTimeSlot = function(id){
            var postConfig = {
                funcName: 'addShiftTimeSlot',
                successMsg: '班型小类时间段添加成功',
                errorMsg: '班型小类时间段添加失败',
                showLoadLayer: false,
                httpPostData: {
                    data: vm.data.smallClassModalData.timeItemsData,
                    fb_shift_type_id: id
                }
            };

            TeacherService.addShiftTimeSlot(postConfig).catch(function(error){
                console.error(error);
            });
        };

    }
}());
